package org.linlinjava.litemall.wx.service;

import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.dao.LitemallBookReviewMapper;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.service.LitemallGoodsProductService;
import org.linlinjava.litemall.db.service.LitemallGoodsService;
import org.linlinjava.litemall.db.service.LitemallTopicService;
import org.linlinjava.litemall.db.service.LitemallUserService;
import org.linlinjava.litemall.db.vo.BookReviewVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@Service
public class WxBookReviewService {
    @Resource
    private LitemallBookReviewMapper bookReviewMapper;
    @Autowired
    private LitemallUserService userService;
    @Autowired
    private LitemallTopicService topicService;
    @Autowired
    private LitemallGoodsProductService goodsProductService;
    @Autowired
    private LitemallGoodsService goodsService;

    public Object create(Integer userId, LitemallBookReview bookReview) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        Integer goodsProductId = bookReview.getGoodsProductId();
        if (goodsProductId == null) {
            return ResponseUtil.fail(400, "请先选择一本书进行评论");
        }
        LitemallGoodsProduct product = goodsProductService.findById(goodsProductId);
        if (product == null) {
            return ResponseUtil.fail(400, "您评价的商品不存在");
        }
        Integer topicId = bookReview.getTopicId();
        if (topicId != null) {
            LitemallTopic topic = topicService.findById(topicId);
            bookReview.setTopicName(topic.getTitle());
        }
        LitemallUser user = userService.findById(userId);
        bookReview.setUserId(userId);
        bookReview.setNickname(user.getNickname());
        bookReview.setAvatar(user.getAvatar());
        bookReview.setAddTime(LocalDateTime.now());
        bookReview.setUpdateTime(LocalDateTime.now());
        bookReviewMapper.insertSelective(bookReview);
        return ResponseUtil.ok(bookReview);
    }

    public int delete(Integer id) {
        return bookReviewMapper.deleteByPrimaryKey(id);
    }

    public Object getDetail(Integer bookReviewId) {
        LitemallBookReview bookReview = bookReviewMapper.selectByPrimaryKey(bookReviewId);
        if (bookReview == null) {
            return ResponseUtil.fail(500, "该书评不存在或已被删除");
        }
        Integer topicId = bookReview.getTopicId();
        LitemallTopic topic = topicService.findById(topicId);
        Integer goodsProductId = bookReview.getGoodsProductId();
        LitemallGoodsProduct goodsProduct = goodsProductService.findById(goodsProductId);
        LitemallGoods goods = goodsService.findById(goodsProduct.getGoodsId());
        Map<String, Object> goodsDetail = new HashMap<>();
        goodsDetail.put("bookReview", bookReview);
        goodsDetail.put("topic", topic);
        goodsDetail.put("goods", goods);
        return ResponseUtil.ok(goodsDetail);
    }

    /**
     * 根据不同状态（0：全部:1：待审核，2：通过，3：未通过）查询list
     *
     * @param status
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    public List<BookReviewVo> queryList(Integer status, Integer page, Integer limit, String sort, String order) {

        LitemallBookReviewExample example = new LitemallBookReviewExample();
        LitemallBookReviewExample.Criteria criteria = example.or();
        switch (status) {
            case 1:
            case 2:
            case 3:
                criteria.andStatusEqualTo(status);
                break;
            case 0:
            default:
                break;
        }
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        PageHelper.startPage(page, limit);
        List<LitemallBookReview> litemallBookReviews = bookReviewMapper.selectByExample(example);

        List<BookReviewVo> bookReviewVoList = new ArrayList<>();
        if (litemallBookReviews == null) {
            return bookReviewVoList;
        }
        litemallBookReviews.forEach(bookReview -> {
            BookReviewVo bookReviewVo = new BookReviewVo();
            bookReviewVo.setId(bookReview.getId());
            bookReviewVo.setGoodsProductId(bookReview.getGoodsProductId());
            bookReviewVo.setContent(bookReview.getContent());
            bookReviewVo.setAvatar(bookReview.getAvatar());
            bookReviewVo.setNickname(bookReview.getNickname());
            bookReviewVo.setPicUrl(bookReview.getPicUrl());
            bookReviewVo.setStatus(bookReview.getStatus());
            bookReviewVo.setUserId(bookReview.getUserId());
            bookReviewVo.setAddTime(bookReview.getAddTime());
            bookReviewVo.setUpdateTime(bookReview.getUpdateTime());
            LitemallTopic topic = topicService.findById(bookReview.getTopicId());
            bookReviewVo.setTopic(topic);
            LitemallGoodsProduct goodsProduct = goodsProductService.findById(bookReview.getGoodsProductId());
            if (goodsProduct != null) {
                bookReviewVo.setGoodsProduct(goodsProduct);
                LitemallGoods goods = goodsService.findById(goodsProduct.getGoodsId());
                bookReviewVo.setGoods(goods);
                bookReviewVoList.add(bookReviewVo);
            }
        });
        return bookReviewVoList;
    }

    /**
     * 根据话题查询
     *
     * @param topicId
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    public List<BookReviewVo> queryByTopic(Integer topicId, Integer page, Integer limit, String sort, String order) {
        LitemallBookReviewExample example = new LitemallBookReviewExample();
        example.or().andTopicIdEqualTo(topicId).andStatusEqualTo(2);
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        PageHelper.startPage(page, limit);
        List<LitemallBookReview> bookReviews = bookReviewMapper.selectByExample(example);
        List<BookReviewVo> bookReviewVoList = new ArrayList<>();
        bookReviews.forEach(bookReview -> {
            BookReviewVo bookReviewVo = new BookReviewVo();
            bookReviewVo.setId(bookReview.getId());
            bookReviewVo.setGoodsProductId(bookReview.getGoodsProductId());
            bookReviewVo.setContent(bookReview.getContent());
            bookReviewVo.setAvatar(bookReview.getAvatar());
            bookReviewVo.setNickname(bookReview.getNickname());
            bookReviewVo.setPicUrl(bookReview.getPicUrl());
            bookReviewVo.setStatus(bookReview.getStatus());
            bookReviewVo.setUserId(bookReview.getUserId());
            bookReviewVo.setAddTime(bookReview.getAddTime());
            bookReviewVo.setUpdateTime(bookReview.getUpdateTime());
            LitemallTopic topic = topicService.findById(bookReview.getTopicId());
            bookReviewVo.setTopic(topic);
            LitemallGoodsProduct goodsProduct = goodsProductService.findById(bookReview.getGoodsProductId());
            if (goodsProduct != null) {
                bookReviewVo.setGoodsProduct(goodsProduct);
                LitemallGoods goods = goodsService.findById(goodsProduct.getGoodsId());
                bookReviewVo.setGoods(goods);
                bookReviewVoList.add(bookReviewVo);
            }
        });
        return bookReviewVoList;

    }


    public List<BookReviewVo> queryListByUserId(Integer userId, Integer page, Integer limit, String sort, String order) {
        LitemallBookReviewExample example = new LitemallBookReviewExample();
        example.or().andUserIdEqualTo(userId);
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        PageHelper.startPage(page, limit);
        List<LitemallBookReview> bookReviews = bookReviewMapper.selectByExample(example);
        List<BookReviewVo> bookReviewVoList = new ArrayList<>();
        bookReviews.forEach(bookReview -> {
            BookReviewVo bookReviewVo = new BookReviewVo();
            BeanUtils.copyProperties(bookReview, bookReviewVo);
            LitemallTopic topic = topicService.findById(bookReview.getTopicId());
            bookReviewVo.setTopic(topic);
            LitemallGoodsProduct goodsProduct = goodsProductService.findById(bookReview.getGoodsProductId());
            if (goodsProduct != null) {
                bookReviewVo.setGoodsProduct(goodsProduct);
                LitemallGoods goods = goodsService.findById(goodsProduct.getGoodsId());
                bookReviewVo.setGoods(goods);
                bookReviewVoList.add(bookReviewVo);
            }
        });
        return bookReviewVoList;
    }

    /**
     * 查询某一本书下面的评论
     *
     * @param goodsId
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    public List<BookReviewVo> queryByGoodsId(Integer goodsId, Integer page, Integer limit, String sort, String order) {
        LitemallGoodsProduct product = goodsProductService.queryByGidAndSpecification(goodsId, "旧");
        if (product == null) {
            return new ArrayList<>();
        }
        LitemallBookReviewExample example = new LitemallBookReviewExample();
        example.or().andGoodsProductIdEqualTo(product.getId()).andStatusEqualTo(2);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        PageHelper.startPage(page, limit);
        List<LitemallBookReview> bookReviews = bookReviewMapper.selectByExample(example);
        List<BookReviewVo> bookReviewVoList = new ArrayList<>();
        bookReviews.forEach(bookReview -> {
            BookReviewVo bookReviewVo = new BookReviewVo();
            bookReviewVo.setId(bookReview.getId());
            bookReviewVo.setGoodsProductId(bookReview.getGoodsProductId());
            bookReviewVo.setContent(bookReview.getContent());
            bookReviewVo.setAvatar(bookReview.getAvatar());
            bookReviewVo.setNickname(bookReview.getNickname());
            bookReviewVo.setPicUrl(bookReview.getPicUrl());
            bookReviewVo.setStatus(bookReview.getStatus());
            bookReviewVo.setUserId(bookReview.getUserId());
            bookReviewVo.setAddTime(bookReview.getAddTime());
            bookReviewVo.setUpdateTime(bookReview.getUpdateTime());
            LitemallTopic topic = topicService.findById(bookReview.getTopicId());
            bookReviewVo.setTopic(topic);
            LitemallGoodsProduct goodsProduct = goodsProductService.findById(bookReview.getGoodsProductId());
            bookReviewVo.setGoodsProduct(goodsProduct);
            LitemallGoods goods = goodsService.findById(goodsProduct.getGoodsId());
            bookReviewVo.setGoods(goods);

            bookReviewVoList.add(bookReviewVo);
        });
        return bookReviewVoList;
    }
}
